package se254;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class GuessTheNumber {

	private int generatedNumber;
	
	public GuessTheNumber(int lower, int upper) {
		generatedNumber = (int)(Math.random() * (upper - lower + 1)) + lower;
	}
	
	public void execute(int n) {
		if (n == generatedNumber) {
			System.out.println("Correct");
		} else if (n > generatedNumber) {
			System.out.println("Too high");
		} else {
			System.out.println("Too low");
		}
		System.out.println("The actual number is " + generatedNumber);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Please input range of numbers");
		
		
		int lower = 0, upper = 0;
		
		while (true) {
			try {
				System.out.print("Lower: ");
				lower = Integer.parseInt(buffer.readLine());
				System.out.print("Upper: ");
				upper = Integer.parseInt(buffer.readLine());
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
			GuessTheNumber guess = new GuessTheNumber(lower, upper);
			System.out.print("Guess the number: ");
			try {
				int n = Integer.parseInt(buffer.readLine());
				guess.execute(n);
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			System.out.print("Play again (y/n): ");
			try {
				char input = buffer.readLine().charAt(0);
				switch (input) {
					case 'y':
					case 'Y':
						break;
					case 'n':
					case 'N':
						return;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

